<?php

namespace app\common\library;

use \Firebase\JWT\JWT;

class Jwta
{
    // 定义密钥
    private static $key = 'a159456159456';

    // 生成 Token
    public static function encode($data)
    {
        $issuedAt = time();
        $expirationTime = $issuedAt + 3600;  // 设置 Token 过期时间为 1 小时
        $payload = [
            'iat' => $issuedAt,
            'exp' => $expirationTime,
            'data' => $data
        ];
        return JWT::encode($payload, self::$key);
    }

    // 解码 Token
    public static function decode($token)
    {
        try {
            $decoded = JWT::decode($token, self::$key, ['HS256']);
            return (array)$decoded->data;
        } catch (\Exception $e) {
            return null;  // Token 无效或过期
        }
    }
}
